package com.mjf.ue.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mjf.ue.domain.U8BarcodeEntity;
import com.mjf.ue.mapper.U8BarcodeMapper;
import com.mjf.ue.service.AsyncUEService;
import com.mjf.ue.service.IU8BarcodeService;
import com.mjf.ue.vo.BarCodeBatchGetParam;
import com.mjf.ue.vo.BarCodeBatchGetReusultVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 * desc
 * </p>
 *
 * @author chenshoumeng
 * @date 2024/12/22 15:36
 */
@Service
public class U8BarcodeServiceImpl extends ServiceImpl<U8BarcodeMapper, U8BarcodeEntity> implements IU8BarcodeService {

	@Autowired
	private AsyncUEService asyncUEService;

	@Override
	@Transactional(rollbackFor = Exception.class)
	public void asyncBarcodeArchive(BarCodeBatchGetParam param) {
		List<U8BarcodeEntity> entities = new ArrayList<>();
		BarCodeBatchGetReusultVO resultVO = asyncUEService.getBarcodeArchive(param);
		List<U8BarcodeEntity> list = list();
		List<String> barcodes = list.stream().map(U8BarcodeEntity::getBarcode).collect(Collectors.toList());
		if (CollectionUtil.isNotEmpty(resultVO.getBarcode())) {
			for (BarCodeBatchGetReusultVO.Barcode barcode : resultVO.getBarcode()) {
				// 过滤掉非产成品和半成品商品
				if (!barcode.getCinvccode().startsWith("04") && !barcode.getCinvccode().startsWith("05")) {
					continue;
				}
				if (barcodes.contains(barcode.getBarcode())) {
					continue;
				}
				U8BarcodeEntity entity = new U8BarcodeEntity();
				BeanUtil.copyProperties(barcode, entity);
				entity.setId(IdUtil.simpleUUID());
				entity.setTimes(LocalDateTime.now());
				// 处理特殊字段
				if (StrUtil.isNotBlank(barcode.getIinvsalecost())) {
					entity.setIinvsalecost(BigDecimal.valueOf(Double.parseDouble(barcode.getIinvsalecost().replaceAll(" ", ""))));
				}
				if (StrUtil.isNotBlank(barcode.getDmdate())) {
					entity.setDmdate(LocalDate.parse(barcode.getDmdate()));
				}
				if (StrUtil.isNotBlank(barcode.getQty())) {
					entity.setQty(BigDecimal.valueOf(Double.parseDouble(barcode.getQty().replaceAll(" ", ""))));
				}
				if (StrUtil.isNotBlank(barcode.getInum())) {
					entity.setInum(BigDecimal.valueOf(Double.parseDouble(barcode.getInum().replaceAll(" ", ""))));
				}
				if (StrUtil.isNotBlank(barcode.getCsrcsubid())) {
					entity.setCsrcsubid(Integer.valueOf(barcode.getCsrcsubid().replaceAll(" ", "")));
				}
				if (StrUtil.isNotBlank(barcode.getIrowno())) {
					entity.setIrowno(Integer.valueOf(barcode.getIrowno().replaceAll(" ", "")));
				}
				entities.add(entity);
			}
			if (CollectionUtil.isNotEmpty(entities)) {
				saveOrUpdateBatch(entities);
			}
		}
	}
}
